<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Models\System;

use Illuminate\Database\Eloquent\Model;

class Permission extends Model
{

    protected $guarded = [];

    /**
     * 自己与自己关联
     * 一级分类下得二级分类
     * 一对一关联
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function children()
    {
        return $this->hasMany(Permission::class, 'parent_id', 'id');
    }

    /**
     * 权限与用户组关联
     * 多对多关联
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }

    /**
     * 静态方法
     * 获取所有权限列表
     *
     * @param array $select
     *
     * @return Permission[]|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    static function all_permission( $select = [ '*' ] )
    {
        $permission = self::with([
            'children' => function ( $query ) use ( $select ) {//二级分类
                $query->select($select);
                $query->orderBy('sort_order')->orderBy('created_at', 'desc');
                $query->with([
                    'children' => function ( $query ) use ( $select ) {//三级分类
                        $query->select($select);
                        $query->orderBy('sort_order')->orderBy('created_at', 'desc');
                    },
                ]);
            },
        ])->where('parent_id', 0)->select($select)->orderBy('sort_order')->orderBy('created_at', 'desc')->get();//一级分类
        return $permission;
    }

    /**
     * 静态方法
     * 判断自己下面是否有子分类
     *
     * @param       $id
     * @param array $select
     *
     * @return bool
     */
    static function check_children( $id, $select = [ '*' ] )
    {
        $permission = self::with([
            'children' => function ( $query ) use ( $select ) {
                $query->select($select);
            },
        ])->select($select)->find($id);
        if ( $permission->children->isEmpty() ) return TRUE;
        return FALSE;
    }

    /**
     *所有菜单 二级
     *
     * @param array $select
     *
     * @return Permission[]|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    static function get_children( $select = [ '*' ] )
    {
        $children_permissions = self::with([
            'children' => function ( $query ) use ( $select ) {
                $query->orderBy('sort_order');
                $query->orderBy('created_at', 'desc');
                $query->select($select);
            },//二级
        ])->where('parent_id', 0)->select($select)->orderBy('sort_order')->orderBy('created_at', 'desc')->get();//一级
        return $children_permissions;
    }

    static function CreateModel( $data )
    {
        return self::create($data);
    }

    static function FindModel( $id, $select = [ '*' ] )
    {
        return self::select($select)->find($id);
    }

    static function DestroyModel( $id )
    {
        return self::destroy($id);
    }

    static function UpdateModel( $where, $data )
    {
        return self::where($where)->update($data);
    }

    static function FirstModel( $where, $with = [], $select = [ '*' ] )
    {
        return self::with($with)->where($where)->select($select)->first();
    }

    static function InsertModel( $data )
    {
        return self::insert($data);
    }

    static function FindOrFailModel( $id )
    {
        return self::findOrFail($id);
    }
}
